package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/19
 */

import com.qf.domain.Goods;
import com.qf.domain.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/19 20:39
 * description:
 */
@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet {
    private GoodsService goodsService = new GoodsServiceImpl();

   public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response)throws IOException {
//       int typeId = Integer.parseInt(request.getParameter("typeId"));
//       String pageNumStr = request.getParameter("pageNum");
//       String pageSizeStr = request.getParameter("pageSize");
//
//       int pageNum = 1;
//       int pageSize = 2;
//       if(!StringUtils.isEmpty(pageNumStr)){
//           pageNum = Integer.parseInt(pageNumStr);
//       }
//
//       if(!StringUtils.isEmpty(pageSizeStr)){
//           pageSize=Integer.parseInt(pageSizeStr);
//       }
//       PageBean pageBean = goodsService.findByPage(typeId,pageNum,pageSize);
//
//       request.setAttribute("poageBean", pageBean);
//
//       return "forward:/goodsList.jsp";
       //获取分类id
       int typeId = Integer.parseInt(request.getParameter("typeId"));
       String pageNumStr = request.getParameter("pageNum");
       String pageSizeStr = request.getParameter("pageSize");
       //默认值
       int pageNum = 1;
       int pageSize = 2;
       if(!StringUtils.isEmpty(pageNumStr)) {
           pageNum = Integer.parseInt(pageNumStr);
       }

       if(!StringUtils.isEmpty(pageSizeStr)) {
           pageSize = Integer.parseInt(pageSizeStr);
       }


       PageBean pageBean = goodsService.findByPage(typeId, pageNum, pageSize);

       request.setAttribute("pageBean", pageBean);
       request.setAttribute("typeId", typeId);

       return "forward:/goodsList.jsp";

   }
   public String getGoodsById(HttpServletRequest request, HttpServletResponse response)throws IOException {
       int id =Integer.parseInt(request.getParameter("id"));
       Goods goods = goodsService.findById(id);
       request.setAttribute("goods", goods);
       return "forward:/goodsDetail.jsp";
   }
}